###########################################################
# Makefile for LaTeX docs

LATEX=pdflatex
FILE=thesis
.SUFFIXES:
.SUFFIXES: .ps .dvi .tex .sty

TEXINPUTS := .:${TEXINPUTS}
BIBINPUTS := .:${TEXINPUTS}

TEX = $(wildcard *.tex)
all: pdf 
	#eps ps dvi pdf 
# view
eps: fig obj

ps:  ${FILE}.ps eps
pdf:  ${FILE}.pdf eps
dvi:  ${FILE}.dvi eps


obj: $(patsubst %.obj, %.eps, $(wildcard *.obj))
fig: $(patsubst %.fig, %.eps, $(wildcard *.fig))

clean:
	$(RM) *.aux *.log *.bbl *.blg *~ \#*\# *.toc *.idx
	$(RM) $(patsubst %.tex, %.ps, $(wildcard *.tex))
	$(RM) $(patsubst %.tex, %.dvi, $(wildcard *.tex))
	$(RM) $(patsubst %.tex, %.pdf, $(wildcard *.tex))

distclean: clean
	$(RM) $(patsubst %.fig, %.eps, $(wildcard *.fig))
	$(RM) $(patsubst %.obj, %.eps, $(wildcard *.obj))

view: ${FILE}.pdf
	open ${FILE}.pdf

count: 
	texcount -total *.tex

upload: 
	scp thesis.pdf ss824@thalassa.cl.cam.ac.uk:/home/ss824/public_html/final_draft.pdf

labcount: 
	ps2ascii ${FILE}.pdf | wc -w

%.pdf: $(TEX) $(wildcard *.bib)
	$(LATEX) $*
	-bibtex $*
	if [ -e $*.toc ] ; then $(LATEX) $* ; fi
	if [ -e $*.bbl ] ; then $(LATEX) $* ; fi
	$(LATEX) $*
	$(LATEX) $*
	if egrep Rerun $*.log ; then $(LATEX) $* ; fi
	if egrep Rerun $*.log ; then $(LATEX) $* ; fi
	if egrep Rerun $*.log ; then $(LATEX) $* ; fi

%.ps: %.dvi
	dvips -tletter -o $@ $<

%.ps.gz: %.ps
	gzip -v9 $<

%.eps: %.obj
	tgif -color -print -eps $*

%.eps: %.fig
	fig2dev -L eps $< $@

toc: $(wildcard *.tex)
	@egrep -h "\\\((sub)*section|chapter)" $+ | egrep -v "^%" | \
                sed -e 's/\\chapter//;                          \
                s/\\section/    /;                              \
                s/\\subsection/         /;                      \
                s/\\subsubsection/                      /;      \
                s/\\.*{//g;s/[{}]//g' > $@
